package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.commons.ResponseResult;
import cn.kgc.springcloud.entity.Tag;
import cn.kgc.springcloud.service.TagService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/5/8
 * @description
 */
@RestController
@RequestMapping("tag")
public class TagController {

    @Resource
    private TagService tagService;
    // 标签列表
    @GetMapping
    public ResponseResult tagList() {
        List<Tag> list = tagService.list();
        return   ResponseResult.success().data(list);
    }


    @GetMapping("{id}")
    public Tag getOneTag(@PathVariable("id") Integer id) {

        Tag tag = tagService.getById(id);
        return  tag;
    }

    // 删除标签
    @DeleteMapping("{id}")
    public ResponseResult deleteTag(@PathVariable("id") Integer id) {
        tagService.removeById(id);
        return   ResponseResult.success().message("删除成功");
    }

    // 添加标签
    @PostMapping
    public ResponseResult addTag(@RequestBody Tag tag) {
        tag.setCreatedate(new Date());
        tagService.save(tag);
        return   ResponseResult.success().message("添加成功").data(tag);
    }
}
